using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

namespace FrameWork.TileMapManager
{
    /// <summary>
    /// 可耕种土地
    /// 开垦后
    /// </summary>
    public class CultivableTileView : MonoBehaviour
    {
        [SerializeField] private Tilemap cultivatableTilemap;
        [SerializeField] private TileBase cultivatableTile;
        
        /// <summary>
        /// 加载地图时候调用
        /// 显示所有可耕种的瓦片图片
        /// </summary>
        /// <param name="cultivatedTiles">可耕种的瓦片集合</param>
        public void CultivatableTileViewInit(MapModel model)
        {
            cultivatableTilemap.ClearAllTiles();
            foreach (var pos in model.CultivatedTiles)
            {
                cultivatableTilemap.SetTile(pos, cultivatableTile);
            }
        }
        
        /// <summary>
        /// 更新UI界面
        /// </summary>
        /// <param name="pos">开垦的瓦片地图坐标</param>
        public void UpdateView(Vector3Int pos)
        {
            // print("开垦土地" + pos);
            cultivatableTilemap.SetTile(pos, cultivatableTile);
        }

        /// <summary>
        /// 坐标转换，世界坐标转换为瓦片地图坐标
        /// </summary>
        /// <param name="worldPosition"></param>
        /// <returns></returns>
        public Vector3Int WorldToCell(Vector3 worldPosition)
        {
            return cultivatableTilemap.WorldToCell(worldPosition);
        }
    }
} 